package com.hspedu.com.jdbc.resultset_;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @author DL5O
 * @version 1.0
 * 演示select 语句并返回 ResultSet，并取出结果
 */
@SuppressWarnings("all")
public class ResultSet_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileReader("src\\jdbc.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        //1.注册驱动
        Class.forName(driver);//建议写上
        //2.得到连接
        Connection connection = DriverManager.getConnection(url, user, password);
        //3.得到statement
        Statement statement = connection.createStatement();
        //4.组织sql
        String sql = "select id,name,sex,borndate from actor";
        ResultSet resultSet = statement.executeQuery(sql);

        //5.使用while循环取出数据
        while(resultSet.next()){//让光标向下移动，如果没有更多行，则返回false
            int id = resultSet.getInt(1);//获取该行的第一列数据
            String name= resultSet.getString(2);//获取该行第二列数据
            String sex = resultSet.getString(3);
            Date date = resultSet.getDate(4);
            System.out.println(id + "\t" + name + "\t" + sex + "\t" + date);
        }

        //6.关闭连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
